package queue;

public class MyQueueUsedLinkedList {

    class ListNode {
        ListNode prev;
        ListNode next;
        int val;
        public ListNode(int val) {
            this.val = val;
        }
    }

    private int usedSize;
    private ListNode front;
    private ListNode rear;

    public MyQueueUsedLinkedList() {
        this.usedSize = 0;
        this.front = null;
        this.rear = null;
    }
    public void offer(int val) {
        ListNode node = new ListNode(val);
        if(front == null) {
            front = rear = node;
        }else {
            rear.next = node;
            node.prev = rear;
            rear = node;
        }
        usedSize++;
    }

    public int poll() {
        if(isEmpty()) {
            throw new NullPointerException("空队列异常");
        }
        int oldVal = front.val;
        front = front.next;
        front.prev = null;
        usedSize--;
        return oldVal;
    }
    public int peek() {
        if(isEmpty()) {
            throw new NullPointerException("空队列异常");
        }
        return front.val;
    }

    public int size() {
        return usedSize;
    }

    public boolean isEmpty() {
        return front == null;
    }
}
